package site.jlopen.secret.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.lang.TypeReference;
import site.jlopen.secret.client.SecretClientHttp;
import site.jlopen.secret.client.SecretReqUrl;
import site.jlopen.secret.common.SecretClientResponse;
import site.jlopen.secret.common.SecretReqBaseForm;
import site.jlopen.secret.config.SecretClientProperties;
import site.jlopen.secret.model.data.SecretDataAllInfoResp;
import site.jlopen.secret.model.data.SecretDataCreateReq;
import site.jlopen.secret.model.data.SecretDataInfoResp;
import site.jlopen.secret.service.intf.SecretDataManage;

public class SecretDataImpl implements SecretDataManage {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final SecretClientProperties clientProperties;
	
	public SecretDataImpl(SecretClientProperties clientProperties) {
		this.clientProperties = clientProperties;
	}

	@Override
	public SecretClientResponse<SecretDataInfoResp> dataCreate(SecretDataCreateReq form) {
		logger.info("上传节点数据请求：{}", form);
		SecretClientHttp<SecretDataInfoResp> clientRequest = new SecretClientHttp<SecretDataInfoResp>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<SecretDataInfoResp>  res = clientRequest.execute(SecretReqUrl.DATA_CREATE_URL, form, new TypeReference<SecretClientResponse<SecretDataInfoResp>>() {});
		return res;
	}

	@Override
	public SecretClientResponse<String> dataUpdate(SecretDataCreateReq form) {
		logger.info("修改节点数据请求：{}", form);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		form.setAppId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DATA_UPDATE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> dataRemove(Long dataId) {
		logger.info("移除节点数据请求：{}", dataId);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("dataId", dataId)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DATA_REMOVE_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<String> dataStatus(Long dataId) {
		logger.info("刷新节点数据请求：{}", dataId);
		SecretClientHttp<String> clientRequest = new SecretClientHttp<String>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("dataId", dataId)
				.appId(clientProperties.getAppId());
		SecretClientResponse<String>  res = clientRequest.execute(SecretReqUrl.DATA_STATUS_URL, form);
		return res;
	}

	@Override
	public SecretClientResponse<SecretDataInfoResp> dataInfo(Long dataId) {
		logger.info("获取节点数据请求：{}", dataId);
		SecretClientHttp<SecretDataInfoResp> clientRequest = new SecretClientHttp<SecretDataInfoResp>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("dataId", dataId)
				.appId(clientProperties.getAppId());
		SecretClientResponse<SecretDataInfoResp>  res = clientRequest.execute(SecretReqUrl.DATA_INFO_URL, form, new TypeReference<SecretClientResponse<SecretDataInfoResp>>() {});
		return res;
	}

	@Override
	public SecretClientResponse<SecretDataAllInfoResp> dataAllInfo(String nodeCode, String domainDataId) {
		logger.info("获取节点数据请求：{}", domainDataId);
		SecretClientHttp<SecretDataAllInfoResp> clientRequest = new SecretClientHttp<SecretDataAllInfoResp>(clientProperties);
		Map<String, Object> form = new SecretReqBaseForm<Object>().add("nodeCode", nodeCode)
				.add("domainDataId", domainDataId)
				.appId(clientProperties.getAppId());
		SecretClientResponse<SecretDataAllInfoResp>  res = clientRequest.execute(SecretReqUrl.DATA_ALL_INFO_URL, form, new TypeReference<SecretClientResponse<SecretDataAllInfoResp>>() {});
		return res;
	}

}
